package cn.dt70.service.impl;

import cn.dt70.dao.aqbUser.AqbUserMapper;
import cn.dt70.pojo.AqbUser;
import cn.dt70.service.AqbUserService;
import cn.dt70.utils.PageSupport;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service("aqbUserService")
public class AqbUserServiceImpl implements AqbUserService {

    private static final Logger LOGGER = Logger.getLogger(AqbUserServiceImpl.class);

    @Resource
    private AqbUserMapper aqbUserMapper;


    @Override
    public AqbUser addUser(AqbUser aqbUser) {
        int row = 0;
        try {
            row=aqbUserMapper.saveUser(aqbUser);
            if (1!=row) {
                LOGGER.debug("aqbUserMapper.saveUser 添加失败");
            } else {
                LOGGER.debug("aqbUserMapper.saveUser 添加成功");
            }

        } catch (Exception e) {
            LOGGER.error("aqbUserMapper.saveUser 添加异常" + e.getMessage());
        }
        return aqbUser;
    }

    @Override
    public AqbUser doLogin(String userCode, String password) {
        AqbUser aqbUser=null;
        try {
            aqbUser = aqbUserMapper.qryByuserCode(userCode);
            if (null == aqbUser) {
                LOGGER.debug("aqbUserMapper.qryByuserCode 查询用户为空!");
            } else {
                if (!password.equals(aqbUser.getPassword())) {
                    LOGGER.debug("aqbUserMapper.qryByuserCode 输入的账号和密码不匹配!");
                    aqbUser=new AqbUser();
                } else {
                    LOGGER.debug("aqbUserMapper.qryByuserCode 登陆成功>>>>>!");
                }
            }
        } catch (Exception e) {
            LOGGER.error("aqbUserMapper.qryByuserCode 异常:" + e.getMessage());
            aqbUser=new AqbUser();
        }
        return aqbUser;
    }

    @Override
    public AqbUser getByUserCode(String userCode) {
        AqbUser aqbUser=null;
        try {
            aqbUser=aqbUserMapper.qryByuserCode(userCode);
            if(null==aqbUser){
                LOGGER.debug("aqbUserMapper.qryByuserCode 查询失败");
                aqbUser=new AqbUser();
            }else{
                LOGGER.debug("aqbUserMapper.qryByuserCode 查询成功");
            }
        } catch (Exception e) {
            LOGGER.debug("aqbUserMapper.qryByuserCode 查询异常"+e.getMessage());
            aqbUser=new AqbUser();
        }
        return aqbUser;
    }

    @Override
    public AqbUser getByUserId(String userId) {
        AqbUser aqbUser=null;
        try {
            aqbUser=aqbUserMapper.qryByUserId(userId);
            if(aqbUser==null){
                LOGGER.debug("aqbUserMapper.qryByUserId 查询失败");
                aqbUser=new AqbUser();
            }else{
                LOGGER.debug("aqbUserMapper.qryByUserId 查询成功");
            }
        } catch (Exception e) {
            LOGGER.debug("aqbUserMapper.qryByUserId 查询异常");
            aqbUser=new AqbUser();
        }
        return aqbUser;
    }

    @Override
    public boolean modifyAqbUser(AqbUser aqbUser) {
        boolean flag=false;
        int row=0;
        try {
            row=aqbUserMapper.updateAqbUser(aqbUser);
            if(row != 1){
                LOGGER.debug("aqbUserMapper.updateAqbUser 修改失败");
            }else{
                LOGGER.debug("aqbUserMapper.updateAqbUser 修改成功");
                flag=true;
            }
        } catch (Exception e) {
            LOGGER.error("aqbUserMapper.updateAqbUser 修改异常"+e.getMessage());
        }
        return flag;
    }



    @Override
    public boolean modifyMoney(Integer money,int userId) {
        int row=0;
        try {
            row=aqbUserMapper.updateMoney2(money,userId);
            if(row !=1){
                LOGGER.debug("aqbUserMapper.updateMoney 修改失败");
            }else{
                LOGGER.debug("aqbUserMapper.updateMoney 修改成功");
                return true;
            }
        } catch (Exception e) {
            LOGGER.debug("aqbUserMapper.updateMoney 修改失败"+e.getMessage());
        }
        return false;
    }

    @Override
    public boolean addemail(String email) {
        int row=0;
        try {
            row=aqbUserMapper.saveemail(email);
            if(row !=1){
                LOGGER.debug("aqbUserMapper.saveemail 添加失败 ");
            }else{
                LOGGER.debug("aqbUserMapper.saveemail 添加成功 ");
                return true;
            }
        } catch (Exception e) {
            LOGGER.debug("aqbUserMapper.saveemail 添加异常 ");
        }
        return false;
    }

    @Override
    public boolean modifyPwd(String password,String userCode) {
        int row=0;
        try {
            row=aqbUserMapper.updatePassword(password,userCode);
            if(row !=1){
                LOGGER.debug("aqbUserMapper.updatePassword 修改失败");
            }else{
                LOGGER.debug("aqbUserMapper.updatePassword 修改成功");
                return true;
            }
        } catch (Exception e) {
            LOGGER.error("aqbUserMapper.updatePassword 修改异常"+e.getMessage());
        }
        return false;
    }


    /* 根据用户名查询邮箱*/
    @Override
    public AqbUser findEmailByUserCode(String userCode,String email) {
        int row=0;
        AqbUser aqbUser=null;
        try {
            row=aqbUserMapper.qryEnailByUserCode(userCode,email);
            if(row !=1){
                LOGGER.debug("aqbUserMapper.qryEnailByUserCode 查询失败");
            }else{
                LOGGER.debug("aqbUserMapper.qryEnailByUserCode 查询成功");
            }
        } catch (Exception e) {
            LOGGER.debug("aqbUserMapper.qryEnailByUserCode 查询异常"+e.getMessage());
        }
        return aqbUser;
    }

    //根据用户编码修改签到信息
    @Override
    public boolean updUserStatusByusercode(String userCode, int status, int count) {
        boolean upp = false;
        try {
            int i = aqbUserMapper.updUserStatusByusercode(userCode, status, count);
            System.out.println(status);
            if (i == 0){
                LOGGER.debug("aqbUserMapper.updUserStatusByusercode 空");
            }else {
                LOGGER.debug("aqbUserMapper.updUserStatusByusercode 成功");
                upp = true;
            }
        } catch (Exception e) {
            LOGGER.error("AqbUserServiceImpl.updUserStatusByusercode 异常"+e.getMessage());
        }
        return upp;
    }

    @Override
    public AqbUser findEmailByUserCode(String userCode) {
        AqbUser aqbUser=null;
        try {
            aqbUser=aqbUserMapper.qryEnailByUserCode(userCode);
            if(aqbUser ==null){
                LOGGER.debug("aqbUserMapper.qryEnailByUserCode 查询失败");
            }else{
                LOGGER.debug("aqbUserMapper.qryEnailByUserCode 查询成功");
            }
        } catch (Exception e) {
            LOGGER.debug("aqbUserMapper.qryEnailByUserCode 查询异常"+e.getMessage());
        }
        return aqbUser;
    }

    //    后台分页查询用户信息
        @Override
        public List<AqbUser> getAllUserInfo(String userCode, String userName, PageSupport ph) {
            List<AqbUser> userinfoList = new ArrayList<>();
//        if("".equals(userCode)){
//            userCode = null;
//        }
            try {
                ph.setTotalCount(aqbUserMapper.qryCount(userCode,userName));
                userinfoList = aqbUserMapper.getAllUserInfo(userCode,userName,(ph.getCurrentPageNo()-1)*ph.getPageSize(),ph.getPageSize());
//           userinfoList = userMapper.getAllUserInfo(userCode,userName,ph);
                if (userinfoList == null){
                    LOGGER.debug("AqbUserServiceImpl.getAllUserInfo 分页查询为空");
                }else {
                    LOGGER.debug("AqbUserServiceImpl.getAllUserInfo 分页查询成功！");
                }
            } catch (Exception e) {
                LOGGER.error("AqbUserServiceImpl.getAllUserInfo 异常"+e.getMessage());
            }
            return userinfoList;
        }

//    根据用户编码获取用户详细信息
        @Override
        public AqbUser getUserInfoById(String userCode){
            AqbUser aqbUser = null;
            try {
                aqbUser = aqbUserMapper.getUserInfoById(userCode);
                if (null == aqbUser){
                    LOGGER.debug("AqbUserServiceImpl.getUserInfoById 为空！");
                }else {
                    LOGGER.debug("AqbUserServiceImpl.getUserInfoById 成功");
                }
            } catch (Exception e) {
                LOGGER.error("AqbUserServiceImpl.getAllUserInfoById 异常"+e.getMessage());
            }
            return aqbUser;
        }

        @Override
        public boolean delUserInfo(String userCode) {
            boolean flag =false;
            try {
                int i = aqbUserMapper.delUserInfo(userCode);
                if (i==0){
                    LOGGER.debug("删除用户失败");
                }else {
                    LOGGER.debug("删除用户成功");
                    flag=true;
                }
            } catch (Exception e) {
                LOGGER.error("删除用户异常："+e.getMessage());
            }
            return flag;
        }
    }
